<?php error_reporting(0) ;
ini_set("display_errors", 0 );
?>
<style>
    #div_esquerda {
        width: 200px;
        float:left;
    }
    #div_direita {
        width: 590px;
        float: right;
    }
</style>
<div align="center">
        <div align="center" id="div_principal">
          <form id="formGrade">
              <table width="787">
<tr>
                      <td width="133">S&eacute;rie: 
      <select id="serieGrade"><?php $this->load->view('serie/cbo_serie') ?></select></td>
      <td width="12"></td>
      <td width="12"></td>
      <td width="12"></td>
      <td width="496"></td>
      <td width="211">
          <div id="divTransfGrade" style="display: none">S&eacute;rie: 
            <select id="serieGradeTransp"><?php $this->load->view('serie/cbo_serie') ?></select>
          </div>
      </td>
                </tr>
                  <tr>
                      <td colspan="5"><input type="checkbox" id="transfGrade" value="1" />Transferir Grade</td>
                      <td>
                          <div id="div_btn_transf" style="display: none" align="center">
                              <input type="button" id="btnTranfGrade" value="Confirmar Transfer&ecirc;ncia" />
                          </div>
                      </td>
                  </tr>
              </table>
            <br /><br />
            <div id="div_esquerda" style="display: none">
                <p style="font-size: 13px"><a id="addTurma">
                        <img src="<?php echo base_url() . 'assets/images/add_database.png' ?>" 
                             width="20" height="20" alt="addTurma" />
                        Adicionar turma
                    </a>
                </p>
                <div id="div_lista_turma"></div>
                <br /><br />
            </div> 
            <div id="div_direita" style="display: none" align="right">
                <p style="font-size: 13px"><a id="addGrade">
                        <img src="<?php echo base_url() . 'assets/images/add_database.png' ?>" 
                             width="20" height="20" alt="addGrade" />
                        Adicionar grade
                    </a>
                </p>
                <div id="div_lista_grade"></div>
            </div>
            <br /><br />
            </form>
          
          <br />

        <br /><br /><br />
<script>
    $(document).ready(function(){
        
        $('#transfGrade').click(function(){
            
            var check = jQuery('input[id="transfGrade"]:checked').val();
            
            if(check == 1){
                
                $('#divTransfGrade').css('display','block');
                
            } else {
                $('#divTransfGrade').css('display','none');
                $('#div_btn_transf').css('display','none');
            }
            
        });
        
        
        $('#serieGradeTransp').change(function(){
            
            var checkSerie = jQuery('select[id="serieGradeTransp"]').val();
            
            if(checkSerie != ''){
                
                $('#div_btn_transf').css('display','block');
                
            } else {
                $('#div_btn_transf').css('display','none');
            }
            
        });
        
        
        $('#serieGrade').change(function (){
            
            var serieCod = jQuery('select[id="serieGrade"]').val();
            
            if(serieCod != '' && serieCod != null)
                $('#div_esquerda').css('display','block');
            else 
                $('#div_esquerda').css('display','none');
            
            $('#div_direita').css('display','none');
            
            jQuery.ajax( {
                type: "POST",
                url: '?c=c_grade_horaria&m=loadTurma',
                cache: false,
                data: {serieCod:serieCod},
                beforeSend: function (){
                    $('#div_body').addClass("div_carregando");
                },
                complete:  function(){
                    $('#div_body').removeClass("div_carregando");

                },
                success: function(data){
                   
                    $('#div_lista_turma').html(data);
                }
            });
            
        });
        
          
        $('#addTurma').click(function (){
            
            var serie = jQuery('select[id="serieGrade"]').val();
            
            jQuery.ajax( {
                type: "POST",
                url: '?c=c_grade_horaria&m=novaTurma',
                cache: false,
                data: {serie:serie},
                beforeSend: function (){
                    $('#div_body').addClass("div_carregando");
                },
                complete:  function(){
                    $('#div_body').removeClass("div_carregando");

                },
                success: function(data){
                    jQuery.facebox(data);
                }
            });
            
        });
        
        $('#addGrade').click(function (){
            
            var serie = jQuery('select[id="serieGrade"]').val();
            var turmaId = jQuery('input[id="chkTurma"]:radio:checked').val();
              
            jQuery.ajax( {
                type: "POST",
                url: '?c=c_grade_horaria&m=novaGrade',
                cache: false,
                data: {serie:serie,turmaId:turmaId},
                beforeSend: function (){
                    $('#div_body').addClass("div_carregando");
                },
                complete:  function(){
                    $('#div_body').removeClass("div_carregando");

                },
                success: function(data){
                    jQuery.facebox(data);
                }
            });
            
        });
        
        
        $('#btnTranfGrade').click(function(){
            
            var serie       = jQuery('select[id="serieGrade"]').val();
            var serieTransf = jQuery('select[id="serieGradeTransp"]').val();
              
            jQuery.ajax( {
                type: "POST",
                url: '?c=c_grade_horaria&m=transferirGrade',
                cache: false,
                data: {serie:serie,serieTransf:serieTransf},
                beforeSend: function (){
                    $('#div_body').addClass("div_carregando");
                },
                complete:  function(){
                    $('#div_body').removeClass("div_carregando");

                },
                success: function(data){

                    if(data == true){
                        
                        $('#divTransfGrade').css('display','none');
                        $('#div_btn_transf').css('display','none');
                        
                        $('#transfGrade').attr('checked',false);
                        
                        alert('Grade transferida com sucesso!');
                        
                    } else {
                        
                        alert('Ocorreu um erro. Por favor tente novamente.');
                        
                    }
                }
            });
            
        
        });
        
    });
</script>        